﻿#region COPYRIGHT© 2005-2013 Phillip Clark. All rights reserved.

// For licensing information see License.txt (MIT style licensing).

#endregion

namespace FlitBit.Parse
{
	/// <summary>
	/// A character match rule that matches any newline sequence.
	/// </summary>
	public class CharRuleNewline : CharRuleExact
	{
		/// <summary>
		/// Creates a new instance.
		/// </summary>
		/// <param name="parts"></param>
		public CharRuleNewline(params CharMatch[] parts)
			: base(parts)
		{}

		/// <summary>
		///   Determines if the instance is equal to another.
		/// </summary>
		/// <param name="obj">the other object</param>
		/// <returns><em>true</em> if equal; otherwise <em>false</em>.</returns>
		public override bool Equals(object obj)
		{
			return obj is CharRuleNewline
				&& this.Equals((CharRuleNewline) obj);
		}

		/// <summary>
		///   Gets the object's hashcode.
		/// </summary>
		/// <returns></returns>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		/// <summary>
		///   Tries to match the sequence at the cursor position, advancing the cursor if successful.
		/// </summary>
		/// <param name="input">the input</param>
		/// <param name="cursor">a reference to the cursor</param>
		/// <returns>
		///   <em>true</em> if the sequence matched at the initial cursor; otherwise <em>false</em>.
		/// </returns>
		public override bool TryAt(char[] input, ref Cursor cursor)
		{
			var cc = cursor;
			if (base.TryAt(input, ref cc))
			{
				cursor = cursor.Newline(cc - cursor.Offset);
				return true;
			}
			return false;
		}

		/// <summary>
		///   Tries to match the sequence at the cursor position, advancing the cursor if successful.
		/// </summary>
		/// <param name="input">the input</param>
		/// <param name="cursor">a reference to the cursor</param>
		/// <returns>
		///   <em>true</em> if the sequence matched at the initial cursor; otherwise <em>false</em>.
		/// </returns>
		public override bool TryAt(string input, ref Cursor cursor)
		{
			var cc = cursor;
			if (base.TryAt(input, ref cc))
			{
				cursor = cursor.Newline(cc - cursor.Offset);
				return true;
			}
			return false;
		}

		/// <summary>
		/// Determines if an instance is equal to another.
		/// </summary>
		/// <param name="other">the other</param>
		/// <returns><em>true</em> if equal; otherwise <em>false</em>.</returns>
		public bool Equals(CharRuleNewline other)
		{
			return Equals((CharRule) other);
		}
	}
}